package com.jc.subject.domain.handler;

import com.jc.subject.common.enums.SubjectTypeFlagEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author gongjiawei
 * @Date 2024/7/9 14:35
 * @注释
 */
@Component
public class HandlerFactory implements InitializingBean {
    @Resource
    private List<SubjectStrategy> subjectStrategies;
    private Map<SubjectTypeFlagEnum,SubjectStrategy> handler=new HashMap<>();

    public  SubjectStrategy getStrategy(int code){
        SubjectTypeFlagEnum byCode = SubjectTypeFlagEnum.getByCode(code);
        return handler.get(byCode);
    }
    @Override
    public void afterPropertiesSet() throws Exception {
       subjectStrategies.forEach(
               subjectStrategy -> handler.put(subjectStrategy.getcode(),subjectStrategy));
    }
}
